package com.gopi.remfilebrowser.gui;

import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import com.gopi.remfilebrowser.util.FileProperty;
import com.gopi.remfilebrowser.util.RemoteFileBrowser;
import com.gopi.utilities.gui.GuiFactory;
import com.gopi.utilities.gui.PropertySheet;

public class FilePropertyDialog 
{
	
	private JDialog dialog;
	private JTextField contains;
	private JTextField fileSize;
	
	private Thread thread;
	
	public FilePropertyDialog(Frame owner)
	{
		dialog = new JDialog(owner);
	}
	
	private void init(final String path,final boolean isRemote,final boolean isFile)
	{
		dialog.setTitle("File Properties");
		dialog.setContentPane(GuiFactory.createGridBagPanel());
		JPanel panel = (JPanel) dialog.getContentPane();
//		panel.setBackground(Color.DARK_GRAY);
		GridBagConstraints gc = new GridBagConstraints();
		gc.gridx = gc.gridy = 0;
		gc.weightx = 0; gc.weighty = 0;
		gc.anchor = GridBagConstraints.NORTHWEST;
		gc.fill = GridBagConstraints.NONE;
		gc.insets.left = gc.insets.right = gc.insets.top = gc.insets.bottom = 10;
		PropertySheet.addEntry(panel,PropertySheet.ICON_AREA,isFile ? "file_big" : "folder_big",
				getFilePart(path),gc);
		
		PropertySheet.addEntry(panel,PropertySheet.SELECTABLE_LABEL,"Location :",
				isRemote ? "Remote Host" : "Local Computer",gc);
		if(!isRemote)
		{
			if(!isFile)
			{
				contains = (JTextField) PropertySheet.addEntry(panel,PropertySheet.SELECTABLE_LABEL,"Contains :",
						"finding ..." ,gc);
			}
			fileSize = (JTextField) PropertySheet.addEntry(panel,PropertySheet.SELECTABLE_LABEL,"Size :",
					"finding ..." ,gc);
			thread = RemoteFileBrowser.getFilePropertyAsync(path, new RemoteFileBrowser.UpdateFilePropertyNotifier()
			{
				public void notifyUpdate(final FileProperty property)
				{
					SwingUtilities.invokeLater(new Runnable()
					{
						public void run()
						{
							if(contains!=null)
								contains.setText(property.getFolderSize());
							if(fileSize!=null)
								fileSize.setText(property.getGoodFileSize());
						}
					});
				}
			});

		}
		else
		{
			
		}
		gc.weighty = 100;
		PropertySheet.addEntry(panel,PropertySheet.SELECTABLE_LABEL,"File Path :",
				getPathPart(path),gc);
		gc.gridx = 1;
		gc.weightx = 50;
		gc.gridwidth = 2;
		gc.gridy++;
		gc.anchor = GridBagConstraints.EAST;
		gc.fill = GridBagConstraints.NONE;
		JButton button ;
		panel.add(button = GuiFactory.createButton("Ok", "", (Icon) null, null),gc);
		button.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				thread = null;
				dialog.dispose();
				dialog.setVisible(false);
			}
		});
		dialog.setSize(300,300);
	}
	
	public void showFileProperty(String path,boolean isRemote,boolean isFile)
	{
		init(path,isRemote,isFile);
		dialog.setVisible(true);
	}
	
	public static String getFilePart(String path)
	{
		String fp = File.separator;
		int lastIndex = path.lastIndexOf(fp);
		if(lastIndex!=-1)
		{
			return path.substring(lastIndex+1);
		}
		return path;
	}
	
	public static String getPathPart(String path)
	{
		String fp = File.separator;
		int lastIndex = path.lastIndexOf(fp);
		if(lastIndex!=-1)
		{
			return path.substring(0,lastIndex);
		}
		return path;
	}
}
